from ConfigParser import ConfigParser
import os

DATA_FILE = "~/.opslog/data.ini"

class _DataManager(object):
    def __init__(self):
        self.data_file = os.path.expanduser(DATA_FILE)
        self.config = ConfigParser()
        if os.path.isfile(self.data_file):
            self.config.read(self.data_file)
    
    def get(self, section, option):
        value = None
        if self.config.has_option(section, option):
            value = self.config.get(section, option)
        return value

    def set(self,section,option,value):
        if not self.config.has_section(section):
            self.config.add_section(section)
        self.config.set(section, option, value)
        self.save()
    
    def remove(self,section,option):
        if self.config.has_section(section) and self.config.has_option(section,option):
            self.config.remove_option(section,option)
            self.save()

    def save(self):
        if not os.path.isfile(self.data_file):
            os.makedirs(os.path.dirname(self.data_file))
        new_file = open(self.data_file,"w")
        self.config.write(new_file)
        new_file.close()
        

DataManager = _DataManager()
